package com.example.day01_weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day01_weather.R;
import com.example.day01_weather.bean.CityBean;
import com.example.day01_weather.bean.WeatherBean;

import java.util.ArrayList;

public class Adapter2 extends RecyclerView.Adapter {
    private ArrayList<CityBean.ResultBean> resultBeans;
    private Context context;

    public Adapter2(ArrayList<CityBean.ResultBean> resultBeans, Context context) {
        this.resultBeans = resultBeans;
        this.context = context;
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.qie, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1= (ViewHolder) holder;
        CityBean.ResultBean bean = resultBeans.get(position);
        holder1.TextView_t1.setText(bean.getCity());
        holder1.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener!=null){
                    onClickListener.ClickListener(position);
                }
            }
        });
    }
    public interface OnClickListener{
        public void ClickListener(int position);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public OnClickListener onClickListener;



    @Override
    public int getItemCount() {
        return resultBeans.size();
    }

    public static
    class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView TextView_t1;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.TextView_t1 = (TextView) rootView.findViewById(R.id.TextView_t1);
        }

    }
}
